<?
	/* View */
	define('PS', 1);
	include_once("../../defines.php");

	include_once('header.php');
	
	$tournament_id = $_REQUEST['id'];
	$PageName = 'tournament';
	include_once(PUBLIC_BEHIND_DIRECTORY . $PageName . '.php');
	
		function GetRoundDescription($fixture, $tournament, $CurrentRound){
			if($tournament->getClass()->getDescription() != 'Nations'){
				if ($fixture->getTotalMatches() <= 8) return $fixture->getDescription();
			
				$totalPlayersInTournament = 0;
				$rounds = $tournament->getFixture();
				foreach ($rounds as $round){
					$matches = $round->getMatches();
					if (count($matches) > 0 && $totalPlayersInTournament == 0){
						$totalPlayersInTournament = $round->getTotalMatches()*2;
					}
				}
				
				$totalPlayersInRound = $fixture->getTotalMatches()*2;
				switch ($totalPlayersInRound/$totalPlayersInTournament){					
					case 1: return 'Primera Ronda';
					case 0.5: return 'Segunda Ronda';
					case 0.25: return 'Tercera Ronda';
				}
			}
		
			if(strstr($tournament->getName(), 'Repechaje'))
				return 'Ronda Eliminatoria';
			
			if(strstr($tournament->getName(), 'Grupo Mundial II'))
				return 'Primera Ronda';

			return $fixture->getDescription();
		}
	
	function renderInternationalMatch($Match, $messages){
		$Flag1 = file_exists(MAIN_IMAGES_FLAG_DIRECTORY . $Match->getCountry1()->getFlag()) ? MAIN_IMAGES_FLAG_PATH . $Match->getCountry1()->getFlag() : NO_IMAGE;
		$Flag2 = file_exists(MAIN_IMAGES_FLAG_DIRECTORY . $Match->getCountry2()->getFlag()) ? MAIN_IMAGES_FLAG_PATH . $Match->getCountry2()->getFlag() : NO_IMAGE;
		
		$html = '<img src="' . $Flag1 . '" class="miniFlag" /><span class="underline">' . $Match->getCountry1()->getName() . ' ' . $Match->getResult() . ' ' . $Match->getCountry2()->getName() . '</span><img src=' . $Flag2 . ' class="miniFlag" /><br />';
		$SingleMatches = $Match->getMatches();
		if (!empty($SingleMatches)){
			foreach ($Match->getMatches() as $SingleMatch){
				if($SingleMatch->getType() == 'S'){
					$html .= '<span class="subMatch">' . 
						$SingleMatch->getPlayer1()->getName() . ' ' . $SingleMatch->getPlayer1()->getSurname() . 
						' - ' . 
						$SingleMatch->getPlayer2()->getName() . ' ' . $SingleMatch->getPlayer2()->getSurname() . ' [ ' .
						$SingleMatch->getResult() . 
					' ]</span><br />';
				}else{
					$html .= '<span class="subMatch">' . 
						$SingleMatch->getPlayer1()->getName() . ' ' . $SingleMatch->getPlayer1()->getSurname() . ' y ' . $SingleMatch->getPlayer3()->getName() . ' ' . $SingleMatch->getPlayer3()->getSurname() . 
						' - ' .
						$SingleMatch->getPlayer2()->getName() . ' ' . $SingleMatch->getPlayer2()->getSurname() . ' y ' . $SingleMatch->getPlayer4()->getName() . ' ' . $SingleMatch->getPlayer4()->getSurname() . ' [ ' .
						$SingleMatch->getResult() . 
						' ]</span><br />';	
				}
			}
		}
		
		return $html . '<br />';
	}
?>

<div id="tournamentContent" class="centerContent">
	<div class="tournamentTitle uppercase">
		<?= $Name ?>
	</div>
	<div class="tournamentContent">
		<?
		if (!empty($Name)){
		?>
		<div id="tournamentInformation">
			<?
			$image = $arrTournament[0]->getImage();
			if (!empty($image)){
			?>
				<img src="<?= MAIN_TOURNAMENTLOGO_NEWS_PATH . $image;?>" />
			<?
			}
			?>
			<div class="right inlineBlock">
				<span class="titleDescription">Superficie:</span> <?=utf8_encode(FunctionsHelper::ifEmpty($arrTournament[0]->getSurface()));?><br /><br />
				<span class="titleDescription">Fecha Inicio:</span> <?=FunctionsHelper::ifEmpty(FunctionsHelper::adaptListFormatDate($arrTournament[0]->getInitialDate()));?><br /><br />
				<span class="titleDescription">Fecha Fin:</span> <?=FunctionsHelper::ifEmpty(FunctionsHelper::adaptListFormatDate($arrTournament[0]->getFinalDate()));?><br /><br />
				<span class="titleDescription">Sexo:</span> 
				<? 
					echo FunctionsHelper::ifEmpty($SEX[$arrTournament[0]->getSex()]);
					if (!empty($BothSex))
						echo " - " . FunctionsHelper::ifEmpty($SEX[$arrTournament[1]->getSex()]);
				?>
				<br /><br />
				<span class="titleDescription">Categor&iacute;a:</span> <?=FunctionsHelper::ifEmpty($arrTournament[0]->getClass()->getDescription());?><br /><br />
				<span class="titleDescription">Puntos:</span> <?=FunctionsHelper::ifEmpty($Tournament->getPoints());?><br /><br />
			</div>
			<br />
			<span class="titleDescription">Descripci&oacute;n:</span> <?=utf8_encode(FunctionsHelper::ifEmpty($arrTournament[0]->getDescription()));?><br /><br />
			<div id="fixture">
				<?
				foreach ($arrTournament as $tournament){
					echo '<div class="tournament-fixture">'; // Opens fixture main div
					if ($nations == 0)
						echo '<span class="titleDescription underline">Cuadro principal ' . $SEX[$tournament->getSex()] . '</span><br />';

					$arrFixture = $tournament->getFixture();
					$totalMatches = 0; $roundCounter = 1; $previousMarginTop = 0;

					foreach ($arrFixture as $Fixture){
						$AmountMatches = count($Fixture->getMatches());
						$totalMatches += $AmountMatches;
						if ($AmountMatches > 0){
					?>
						<div class="round<?= $nations==0 ? ' normal' : '';?> ">
							<span class="titleDescription"><?= GetRoundDescription($Fixture, $tournament, $roundCounter);?></span><br />
					<?
							if ($nations == 1){
								foreach($Fixture->getMatches() as $Match){
									echo renderInternationalMatch($Match, $messages);									
								}
							}else{
									$currentMarginTop = $roundCounter > 1 ? $previousMarginTop + 54/2 : 0;
					
									$matchCounter = 1;
									foreach ($Fixture->getMatches() as $Match){
										if ($matchCounter > 1 && $roundCounter > 1)
											$currentMarginTop = pow(2, $roundCounter)*32 - 54;
						?>
										<div class="single-match" style="margin-top:<?=$currentMarginTop;?>px; margin-bottom:<?=$roundCounter == 1 ? "10" : "0";?>px;">
											<div class="first-player"><span><?= $Match->getPlayer1()->getName() . ' ' . $Match->getPlayer1()->getSurname();?></span></div>
											<div class="result"><span><?= $Match->getPlayer2()->getName() != "BYE" && $Match->getPlayer2()->getName() != "Qualy" ? $Match->getResult() : ''; ?></span></div>
											<div class="second-player"><span><?= $Match->getPlayer2()->getName() . ($Match->getPlayer2()->getName() != "BYE" || $Match->getPlayer2()->getName() != "Qualy" ? ' ' . $Match->getPlayer2()->getSurname() : '');?></span></div>
										</div>
					<?
										$matchCounter++;
									}
								$previousMarginTop = $currentMarginTop;
							}
						?>
								</div>
						<?
							$roundCounter++;
						}
					}
					echo '<br /><br />';
					
					echo '<div class="clear"></div></div><br />'; //Closes fixture main div
				}
				?>
			</div>
		</div>
		<?
		}else{
			echo '<h3 class="centerContent bold empty">' . $messages->getMessage("TournamentNotLoaded") . '</h3>';
		}
		?>
		
		<div class="clear"></div>
	</div>
</div>

<? include('footer.php'); ?>